package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.vo.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {

    //进行dubbo调用
    @Reference(version = "1.0")
    private UserService userService;

    @GetMapping("find")
    public User find(){
        return userService.find();
    }
    @GetMapping("userInfo")
    public Result userInfo(){
        //获取登录的用户
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        UserDetails principal = (UserDetails)authentication.getPrincipal();
        return Result.success(principal.getUsername());
    }
}
